package com.liang.lessonIp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//客户端
public class TcpClientDemo1 {

    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        //1.要知道服务器地址
        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
        int port = 9999;
        //2.创建一个socket连接
             socket = new Socket(inetAddress, port);
            //3.发送消息io流
             os = socket.getOutputStream();
            os.write("hello world+++梁伟浩".getBytes());

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }
}
